﻿@model Bricks.Site.Models.SelectConstructionContractEstimateView
@{
    ViewBag.Title = "ConstructionContractEstimateList";
    Layout = "~/Views/Shared/_UserPage.cshtml";
}

<head>
    <title></title>
    <script type="text/javascript">
        $(function () {
            //当页面首次刷新的时候执行的事件
            $('#hExpenseBillListJson').val(window.parent.GetExpenseBillListJson())
            initTable();
        });
        //实现对DataGird控件的绑定操作

        function initTable() {
            var contractID = $("#hContractID").val();
            var handleState = $("#hHandleState").val();
            var sourceType = $("#hSourceType").val();
            var processType = $("#hProcessType").val();
            var associatedNodeCode = $("#hAssociatedNodeCode").val();
            var lastUpdateUserDepartment = $("#hLastUpdateUserDepartment").val();
            var lastUpdateUser = $("#hLastUpdateUser").val();
            var expenseID = $("#hExpenseID").val();
            $('#myDataGrid').datagrid({
                title: '',
                //url: '/Expense/GetConstructionContractEstimateList?contractID=' + contractID + "&sourceType=" + sourceType + "&processType=" + processType + "&associatedNodeCode=" + associatedNodeCode + "&lastUpdateUserDepartment=" + lastUpdateUserDepartment + "&lastUpdateUser=" + lastUpdateUser + "&expenseID=" + expenseID,
                url: '/Expense/GetVMEstimateDataList?sectionID=' + contractID,
                nowrap: true,
                autoRowHeight: false,
                striped: true,
                collapsible: true,
                rownumbers: true,
                remoteSort: false,
                fitColumns: true,
                idField: 'ID',
                singleSelect: true,
                //columns: [[
                //    { title: 'ID', field: 'ID', hidden: true },
                //    {
                //        title: '选择', field: 'IsChecked',
                //        formatter: function (value, row, index) {
                //            if (value == true) {
                //                if (handleState == "0") {
                //                    return '<input type="checkbox" checked="true" onclick="ChangeChecked(' + index + ');" />';
                //                }
                //                else {
                //                    return '<input type="checkbox" checked="true" disabled="true" />';
                //                }
                //            }
                //            else {
                //                if (handleState == "0") {
                //                    return '<input type="checkbox"  onclick="ChangeChecked(' + index + ');" />';
                //                }
                //                else {
                //                    return '<input type="checkbox" disabled="true" />';
                //                }
                //            }
                //        }
                //    },
                //    { title: '期次', field: 'ContractStages', width: 50, sortable: true },
                //    { title: '批复金额', field: 'ContractStagesMoney', width: 70, sortable: true },
                //    { title: '已付款金额', field: 'ApprovalMoney', width: 70, sortable: true },
                //    { title: '已付款次数', field: 'ApprovalNum', width: 70, sortable: true },
                //    { title: '本次申请金额', field: 'RequestMoney', width: 70, sortable: true, editor: 'numberbox' },
                //]],
                columns: [[
                    //{
                    //    title: '选择', field: 'IsChecked',
                    //    formatter: function (value, row, index) {
                    //        if (value == true) {
                    //            if (handleState == "0") {
                    //                return '<input type="checkbox" checked="true" onclick="ChangeChecked(' + index + ');" />';
                    //            }
                    //            else {
                    //                return '<input type="checkbox" checked="true" disabled="true" />';
                    //            }
                    //        }
                    //        else {
                    //            if (handleState == "0") {
                    //                return '<input type="checkbox"  onclick="ChangeChecked(' + index + ');" />';
                    //            }
                    //            else {
                    //                return '<input type="checkbox" disabled="true" />';
                    //            }
                    //        }
                    //    }
                    //},
                    { title: '期次', field: 'PeriodID', width: 50, sortable: false },
                    { title: '批复金额', field: 'ApproveMoney', width: 70, sortable: false },
                    { title: '申请状态', field: 'ApplyStatus', width: 70, sortable: false }
                ]],
                onBeforeEdit: function (currentIndex, row) {
                    row.editing = true;
                    $('#myDataGrid').datagrid('refreshRow', currentIndex);
                },
                onLoadSuccess: function (data) {
                    var constructionContractEstimateList = data.rows;
                    var expenseBillListJson = $("#hExpenseBillListJson").val();
                    if (expenseBillListJson != '') {
                        $("input:checkbox").each(function () {
                            $(this)[0].checked = false;
                        });

                        for (var i = 0; i < constructionContractEstimateList.length; i++) {
                            constructionContractEstimateList[i].RequestMoney = null;
                            constructionContractEstimateList[i].IsChecked = false;
                            $('#myDataGrid').datagrid('refreshRow', i);
                        }

                        expenseBillListJson = JSON.parse(expenseBillListJson);
                        for (var i = 0; i < expenseBillListJson.length; i++) {
                            for (var j = 0; j < constructionContractEstimateList.length; j++) {
                                if (expenseBillListJson[i].ContractStages == constructionContractEstimateList[j].ContractStages) {
                                    constructionContractEstimateList[j].RequestMoney = expenseBillListJson[i].RequestMoney;
                                    constructionContractEstimateList[j].IsChecked = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (sourceType != "0") {
                        for (var i = 0; i < constructionContractEstimateList.length; i++) {
                            if (constructionContractEstimateList[i].IsChecked) {
                                $('#myDataGrid').datagrid('beginEdit', i);
                            }
                        }
                    }
                }
            });
        }

        //function ChangeChecked(index) {
        //    $("#myDataGrid").datagrid("selectRow", index);  //选中指定行
        //    var selectedRow = $("#myDataGrid").datagrid("getSelected");  //获取选中行记录
        //    if (selectedRow) {
        //        //此处是为了取出IsChecked的值
        //        if (selectedRow.IsChecked == false) {
        //            selectedRow.IsChecked = true;
        //            $('#myDataGrid').datagrid('beginEdit', index);
        //        }
        //        else {
        //            selectedRow.IsChecked = false;
        //            selectedRow.RequestMoney = null;
        //            $('#myDataGrid').datagrid('cancelEdit', index);
        //        }
        //    }
        //}

        function ConfirmData() {
            //debugger;
            var data = [];
            var hasChecked = false;
            var sumMoney = 0;
            //期次
            var contractStages = 0;

            //$('#myDataGrid').datagrid('acceptChanges');
            //var constructionContractEstimateList = $('#myDataGrid').datagrid('getData').rows;  //取得所有数据
            //for (var i = 0; i < constructionContractEstimateList.length; i++) {
            //    if (constructionContractEstimateList[i].IsChecked) {
            //        var maxMoney = constructionContractEstimateList[i].ContractStagesMoney - constructionContractEstimateList[i].ApprovalMoney;
            //        if (constructionContractEstimateList[i].RequestMoney > 0 && constructionContractEstimateList[i].RequestMoney <= maxMoney) {
            //            var row = { ContractStages: constructionContractEstimateList[i].ContractStages, RequestMoney: constructionContractEstimateList[i].RequestMoney };
            //            data.push(row);
            //            sumMoney += parseFloat(constructionContractEstimateList[i].RequestMoney);
            //        }
            //        else {
            //            for (var j = 0; j < constructionContractEstimateList.length; j++) {
            //                if (constructionContractEstimateList[j].IsChecked) {
            //                    $('#myDataGrid').datagrid('beginEdit', j);
            //                }
            //            }
            //            $.EasyuiSlide("本次申请金额必须大于0,小于等于批复金额减去已付款金额！");
            //            return;
            //        }
            //        hasChecked = true;
            //        //给选择的期次赋值
            //        contractStages = constructionContractEstimateList[i].ContractStages;
            //    }
            //}
            //if (hasChecked == false) {
            //    $.EasyuiSlide("请选择计量期！");
            //    return;
            //}

            //取得选中行
            var selectedRow = $('#myDataGrid').datagrid('getSelected');
            if (selectedRow != null) {
                contractStages = selectedRow.PeriodID;
                sumMoney = selectedRow.ApproveMoney;
                if (sumMoney == 0) {
                    $.EasyuiSlide("选择的期次尚未批复，不可申请！");
                    return;
                }
                else if (selectedRow.ApplyStatus == '已申请') {
                    $.EasyuiSlide("选择的期次已申请，不可重复申请！");
                    return;
                }
            }
            else {
                $.EasyuiSlide("请选择期次期！");
                return;
            }

            var dataJson = JSON.stringify(data);
            window.parent.SetApprovalMoneyAndGetExpenseBillListJson(sumMoney, dataJson, contractStages);
            window.parent.CloseWindow(0);
        }
    </script>
</head>
<body>
    <div style="padding:5px;">
        <input type="hidden" name="ContractID" id="hContractID" value="@Model.ContractID" />
        <input type="hidden" name="HandleState" id="hHandleState" value="@Model.HandleState" />
        <input type="hidden" name="SourceType" id="hSourceType" value="@Model.SourceType" />
        <input type="hidden" name="ProcessType" id="hProcessType" value="@Model.ProcessType" />
        <input type="hidden" name="AssociatedNodeCode" id="hAssociatedNodeCode" value="@Model.AssociatedNodeCode" />
        <input type="hidden" name="LastUpdateUserDepartment" id="hLastUpdateUserDepartment" value="@Model.LastUpdateUserDepartment" />
        <input type="hidden" name="LastUpdateUser" id="hLastUpdateUser" value="@Model.LastUpdateUser" />
        <input type="hidden" name="ExpenseID" id="hExpenseID" value="@Model.ExpenseID" />
        <input type="hidden" id="hExpenseBillListJson" name="ExpenseBillListJson" value="@Model.ExpenseBillListJson" />
        <div region="top" style="height:340px;">
            <table id="myDataGrid" title="" fit="true"></table>
        </div>
        <div style="margin-left: 5px; margin-top: 10px; text-align: right;">
            <input type="button" id="btnSave" value="确定" onclick="ConfirmData();" />
            <input type="button" id="btnCancel" value="取消" onclick="window.parent.CloseWindow(0);" />
        </div>
    </div>
</body>
